import Vue from 'vue';
import TIM from 'tim-js-sdk';
import {
	timeToChat
} from '@/utils/index'
import {
	decodeElement
} from '@/utils/decodeElement'

export default {
	namespaced: true,
	state: {
		allConversation: [], // 所有的conversation
		currentConversationID: '', // 当前聊天对话ID
		currentConversation: {}, // 当前聊天对话信息
		currentMessageList: [], // 当前聊天消息列表
		nextReqMessageID: '', // 下一条消息标志
		isCompleted: false, // 当前会话消息是否已经请求完毕
		isLoading: false, // 是否正在请求
		currentMemberList: [], //当前群成员
	},
	getters: {
		allConversation: state => state.allConversation,
		// 当前聊天对象的ID
		toAccount: state => {
			console.log(state.currentConversationID)
			if (state.currentConversationID.indexOf('C2C') === 0) {
				return state.currentConversationID.substring(3)
			} else if (state.currentConversationID.indexOf('GROUP') === 0) {
				return state.currentConversationID.substring(5)
			}
		},
		// 当前聊天对象的昵称
		toName: state => {
			if (state.currentConversation.type === 'C2C') {
				return state.currentConversation.userProfile.userID
			} else if (state.currentConversation.type === 'GROUP') {
				return state.currentConversation.groupProfile.name
			}
		},
		// 当前聊天对话的Type
		currentConversationType: state => {
			if (state.currentConversationID.indexOf('C2C') === 0) {
				return 'C2C'
			}
			if (state.currentConversationID.indexOf('GROUP') === 0) {
				return 'GROUP'
			}
			return ''
		},
		currentConversation: state => state.currentConversation,
		currentMessageList: state => state.currentMessageList,
		totalUnreadCount: state => {
			const result = state.allConversation.reduce((count, {
				unreadCount
			}) => count + unreadCount, 0)
			return result
		},
		currentMemberList: state => state.currentMemberList,
	},
	mutations: {
		// 历史头插消息列表
		unshiftMessageList(state, messageList) {
			// console.log(messageList)
			let list = [...messageList]
			for (let i = 0; i < list.length; i++) {
				let message = list[i]
				list[i].virtualDom = decodeElement(message)
				// let date = new Date(message.time)
				list[i].newtime = timeToChat(message.time)
			}
			state.currentMessageList = [...list, ...state.currentMessageList]
		},
		// 收到
		receiveMessage(state, messageList) {
			let list = [...messageList]
			for (let i = 0; i < list.length; i++) {
				let message = list[i]
				list[i].virtualDom = decodeElement(message)
				// let date = new Date(message.time * 1000)
				list[i].newtime = timeToChat(message.time)
			}
			state.currentMessageList = [...state.currentMessageList, ...list]
		},
		sendMessage(state, message) {
			message.virtualDom = decodeElement(message)
			// let date = new Date(message.time * 1000)
			message.newtime = timeToChat(message.time * 1000)
			state.currentMessageList.push(message)
		},
		// 更新当前的会话
		updateCurrentConversation(state, conversation) {
			state.currentConversation = conversation
			state.currentConversationID = conversation.conversationID
		},
		// 更新当前所有会话列表
		updateAllConversation(state, list) {
			for (let i = 0; i < list.length; i++) {
				if (list[i].lastMessage && (typeof list[i].lastMessage.lastTime === 'number')) {
					// let date = new Date(list[i].lastMessage.lastTime * 1000)
					list[i].lastMessage._lastTime = timeToChat(list[i].lastMessage.lastTime)
				}
			}
			state.allConversation = list
			let unreadCount = 0
			list.forEach(item => {
				unreadCount += item.unreadCount
			})
			console.log(uni.getStorageSync('tabbarnum') - 1)
			// unreadCount ?state.$commit('setcount',{ index: uni.getStorageSync('tabbarnum') - 1, text: String(unreadCount) }):state.$commit('setcount',{ index: uni.getStorageSync('tabbarnum') - 1, text: '' })
			// #ifdef APP-PLUS
			if (!unreadCount) unreadCount = -1
			plus.runtime.setBadgeNumber(Number(unreadCount));
			// #endif
		},
		// 重置当前会话
		resetCurrentConversation(state) {
			state.currentConversationID = '' // 当前聊天对话ID
			state.currentConversation = {} // 当前聊天对话信息
			state.currentMessageList = [] // 当前聊天消息列表
			state.nextReqMessageID = '' // 下一条消息标志
			state.isCompleted = false // 当前会话消息是否已经请求完毕
			state.isLoading = false // 是否正在请求
		},
		resetAllConversation(state) {
			state.allConversation = []
		},
		removeMessage(state, message) {
			state.currentMessageList.splice(state.currentMessageList.findIndex(item => item.ID === message.ID), 1)
		},
		changeMessageStatus(state, index) {
			state.currentMessageList[index].status = 'fail'
		},
		updateCurrentMemberList(state, list) {
			state.currentMemberList = list
		}
	},
	actions: {
		// 消息事件
		onMessageEvent(context, event) {
			if (event.name === 'onMessageReceived') {
				let id = context.state.currentConversationID
				if (!id) {
					return
				}
				let list = []
				event.data.forEach(item => {
					if (item.conversationID === id) {
						list.push(item)
					}
				})
				context.commit('receiveMessage', list)
			}
		},
		// 获取消息列表
		getMessageList(context) {
			const {
				currentConversationID,
				nextReqMessageID
			} = context.state
			// 判断是否拉完了，isCompleted 的话要报一下没有更多了
			if (!context.state.isCompleted) {
				// 如果请求还没回来，又拉，此时做一下防御
				if (!context.state.isLoading) {
					context.state.isLoading = true
					uni.TUIChatKit.chat.getMessageList({
						conversationID: currentConversationID,
						nextReqMessageID: nextReqMessageID,
						count: 15
					}).then(res => {
						context.state.nextReqMessageID = res.data.nextReqMessageID
						context.commit('unshiftMessageList', res.data.messageList)
						if (res.data.isCompleted) {
							context.state.isCompleted = true
						}
						context.state.isLoading = false
					}).catch(err => {
						console.log(err)
					})
				} else {
					uni.showToast({
						title: '你拉的太快了',
						icon: 'none',
						duration: 500
					})
				}
			} else {
				uni.showToast({
					title: '没有更多啦',
					icon: 'none',
					duration: 1500
				})
			}
		},
		// 获取会话列表
		getConversationList(context) {
			uni.TUIChatKit.chat.getConversationList().then(event => {
				context.commit('updateAllConversation', event.data.conversationList)
			}).catch(err => {
				console.log(err)
			})
		},
		// 获取群成员
		getGroupMemberList({
			state,
			commit
		}) {
			const {
				groupID
			} = state.currentConversation.groupProfile
			return new Promise(reslove => {
				uni.TUIChatKit.chat.getGroupMemberList({
						groupID,
						count: 10,
						offset: 0
					})
					.then(event => {
						const {
							memberList
						} = event.data
						commit('updateCurrentMemberList', memberList)
						reslove()
					})
			})
		},
		checkoutConversation(context, obj) {
			let conversationID = obj.ID
			console.log(conversationID)
			context.commit('resetCurrentConversation')
			uni.TUIChatKit.chat.setMessageRead({
				conversationID
			})
			return uni.TUIChatKit.chat.getConversationProfile(conversationID)
				.then(({
					data: {
						conversation
					}
				}) => {
					context.commit('updateCurrentConversation', conversation)
					context.dispatch('getMessageList')
					let name = ''
					switch (conversation.type) {
						case TIM.TYPES.CONV_C2C:
							name = conversation.userProfile.nick || conversation.userProfile.userID
							break
						case TIM.TYPES.CONV_GROUP:
							name = conversation.groupProfile.name || conversation.groupProfile.groupID
							break
						default:
							name = '系统通知'

					}
					return Promise.resolve()
				})
		}
	}
};
